package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianPvpBestBattleRecord;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianPvpEndRecord;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXiuXianPvpEndRecordDaoImpl extends TempDataObjectDao<CrossXiuXianPvpEndRecord> {

    public static String getCrossXiuXianPvpEndRecordSql = "select * from `t_c_xiuxian_pvp_end_record` where `activityId`=?";

    public Map<Integer, CrossXiuXianPvpEndRecord> getCrossXiuXianPvpEndRecordMap(int activityId) {
        Map<Integer, CrossXiuXianPvpEndRecord> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossXiuXianPvpEndRecordSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXiuXianPvpEndRecord parse = parse(rs);
                map.put(parse.getRound(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossXiuXianPvpEndRecordSql = "insert into `t_c_xiuxian_pvp_end_record`(activityId,round,bestSeizeUser,bestKillUser,campBestSeizeUserMap,campBestKillUserMap,endTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossXiuXianPvpEndRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBestSeizeUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBestKillUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCampBestSeizeUserMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCampBestKillUserMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEndTime()));
        if (getDb().executeNoneQuery(insertCrossXiuXianPvpEndRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossXiuXianPvpEndRecordSql = "update `t_c_xiuxian_pvp_end_record` set bestSeizeUser=?,bestKillUser=?,campBestSeizeUserMap=?,campBestKillUserMap=?,endTime=? where activityId=? and round=?";

    @Override
    protected boolean updateData(CrossXiuXianPvpEndRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBestSeizeUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBestKillUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCampBestSeizeUserMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCampBestKillUserMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        if (getDb().executeNoneQuery(updateCrossXiuXianPvpEndRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossXiuXianPvpEndRecord parseData(ResultSet rs) throws SQLException {
        CrossXiuXianPvpEndRecord data = new CrossXiuXianPvpEndRecord();
        data.setActivityId(rs.getInt("activityId"));
        data.setRound(rs.getInt("round"));
        data.setBestSeizeUser(JSON.parseObject(rs.getString("bestSeizeUser"), new TypeReference<CrossXiuXianPvpBestBattleRecord>() {
        }));
        data.setBestKillUser(JSON.parseObject(rs.getString("bestKillUser"), new TypeReference<CrossXiuXianPvpBestBattleRecord>() {
        }));
        data.setCampBestSeizeUserMap(JSON.parseObject(rs.getString("campBestSeizeUserMap"), new TypeReference<ConcurrentHashMap<Integer, CrossXiuXianPvpBestBattleRecord>>() {
        }));
        data.setCampBestKillUserMap(JSON.parseObject(rs.getString("campBestKillUserMap"), new TypeReference<ConcurrentHashMap<Integer, CrossXiuXianPvpBestBattleRecord>>() {
        }));
        data.setEndTime(rs.getLong("endTime"));
        return data;
    }
}
